<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery-1.4.2.js" charset="utf-8"></script>
<script type="text/javascript" src="js/FreshBooks2.1.js" charset="utf-8"></script>
<script type="text/javascript" src="js/FreshBooksTS.js" charset="utf-8"></script>
<script type="text/javascript">
	var myProjects;
	var myClients;
	var refreshCallback = null;
	var interval;
	var	start;

	function load(){
		var running;
		var pause;

		loadClients();
		loadProjects();

		if (localStorage.getItem("com.yogs.chrome.freshbooks.running")!=null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.running")!=""){
			running = localStorage.getItem("com.yogs.chrome.freshbooks.running");
		}

		//Did they pause
		if (localStorage.getItem("com.yogs.chrome.freshbooks.pause")!=null && 
				localStorage.getItem("com.yogs.chrome.freshbooks.pause")!=""){
			start = new Date(parseInt(localStorage.getItem("com.yogs.chrome.freshbooks.start")));
			pauseTime = new Date(parseInt(localStorage.getItem("com.yogs.chrome.freshbooks.pause")));

			var timeBeforePause = (pauseTime.getTime() - start.getTime());
			var now = new Date();
			//update the stored values
			start = new Date((now.getTime() - timeBeforePause));
			
			var hrs = timeBeforePause/3600000;
			chrome.browserAction.setBadgeText({text:String(hrs.toFixed(2))});
		}
		else if (localStorage.getItem("com.yogs.chrome.freshbooks.start")!=null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.start")!=""){
			start = new Date(parseInt(localStorage.getItem("com.yogs.chrome.freshbooks.start")));
			refresh(); 			
		}
		
	}

	function loadClients()
	{
	    var login = "";
		var token = "";

		if (localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != ""){
			token = localStorage.getItem("com.yogs.chrome.freshbooks.login_key");
		}
		if (localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") !=""){
			login = localStorage.getItem("com.yogs.chrome.freshbooks.logon_url");
		}

		if (!validateLogin(login)) { alert("Invalid login"); return false; }
		if (!validateToken(token)) { alert("Invalid token"); return false; }
		
		com.freshbooks.api.fetchFullList(login,token,'client',{},xmlTimeout,
			function (list) {
				if (list.status.ok) {
					if (debug) alert("Got client list OK");
					
					myClients = list.list;

					if (debug) console.log(myClients);					
				}
				else
				{
					errorLoading(list);
				}
			});		
	}

	
	function loadProjects()
	{
	    var login = "";
		var token = "";

		if (localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != ""){
			token = localStorage.getItem("com.yogs.chrome.freshbooks.login_key");
		}
		if (localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") !=""){
			login = localStorage.getItem("com.yogs.chrome.freshbooks.logon_url");
		}

		if (!validateLogin(login)) { alert("Invalid login, you must go to the options pages and enter a valid Freshbooks login"); return false; }
		if (!validateToken(token)) { alert("Invalid token, you must go to the options pages and enter a valid Freshbooks token"); return false; }
		
		com.freshbooks.api.fetchFullList(login,token,'project',{},xmlTimeout,
			function (list) {
				if (list.status.ok) {
					if (debug) alert("Got project list OK");
					
					myProjects = list.list;
					loadTasks();
					if (debug) console.log(myProjects);
					
				}
				else
				{
					errorLoading(list);
				}
			});		
	}

	function loadTasks()
	{
		var taskCount = 0;       // Semaphore for closures so they know when everyone's done
	    var login = "";
		var token = "";

		if (localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.login_key") != ""){
			token = localStorage.getItem("com.yogs.chrome.freshbooks.login_key");
		}
		if (localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.logon_url") !=""){
			login = localStorage.getItem("com.yogs.chrome.freshbooks.logon_url");
		}
		
		if (!validateLogin(login)) { alert("Invalid login"); return false; }
		if (!validateToken(token)) { alert("Invalid token"); return false; }
		
		for (pid in myProjects) { taskCount++; }
		if (taskCount == 0) {
			alert("No projects found.  Add some!");
			return false;
		}

		for (pid in myProjects)
		{
			com.freshbooks.api.fetchFullList(login,token,'task',{project_id:pid},xmlTimeout,
				(function(proj_id){ return function (list) {
					taskCount--; // This is safe because JS is not multithreaded.
					if (list.status.ok) {
						myProjects[proj_id].tasks = list.list;
					} else {
						alert("Error loading a project's tasks.");
					}

					if (taskCount) {
						if (debug) alert("Loading " + taskCount + " more...");
					} else {
						//finished fetching so see if we need to refresh the lists on options page
						if (refreshCallback != null){
							refreshCallback();
							refreshCallback=null;
						}
					}

					
				}})(pid));
		}
	}

	//The background page takes care of the running update of the icon/badge text
	function refresh(){
		interval= setInterval ( "refresh_interval();", 250 );
	}
	
	function refresh_interval(){
		var now = new Date();
		var autoStop;

		//addition of 500ms displays chosen time before starting to record
		var elapsed = (now.getTime() - start.getTime() + 500);

		var hrs = elapsed/3600000;

		if (localStorage.getItem("com.yogs.chrome.freshbooks.auto_stop") != null &&
				localStorage.getItem("com.yogs.chrome.freshbooks.auto_stop") != ""){
			autoStop = localStorage.getItem("com.yogs.chrome.freshbooks.auto_stop");

			//If the user has set an auto-stop then pause the timer when its reached.
			if (Number(hrs) >= Number(autoStop)){
				pause();
				localStorage.setItem("com.yogs.chrome.freshbooks.pause", now.getTime());	
				localStorage.removeItem("com.yogs.chrome.freshbooks.running");
				
				//Need to set the start Time so the timer only records the autoStop # hours.
				var autoStopMiliSeconds = (autoStop * 3600000);
				start = new Date((now.getTime() - Number(autoStopMiliSeconds)));
				localStorage.setItem("com.yogs.chrome.freshbooks.start", start.getTime());
						
				chrome.browserAction.setBadgeText({text:String(Number(autoStop).toFixed(2))});
			} else {
				chrome.browserAction.setBadgeText({text:String(hrs.toFixed(2))});
			}		
		} else {
			chrome.browserAction.setBadgeText({text:String(hrs.toFixed(2))});
		}

	}

	//Cancel the timer thats updating the Icon text
	function reset(){
		start = null;
		chrome.browserAction.setBadgeText({text: ""});		
		clearInterval(interval);
	}
		
	function pause(){
		clearInterval(interval);
	}

	load();	
	
</script>
</head>
<body>

</body>
</html>